package Region.socket;

import Region.DatabaseManager;
import Region.LogManager;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class RegionToRegionSocketManager implements Runnable{
    private DatabaseManager dataBaseManager;
    private LogManager logManager;
    private ServerSocket serverSocket;

    public RegionToRegionSocketManager(int port, DatabaseManager dataBaseManager, LogManager logManager) throws IOException {
        this.serverSocket = new ServerSocket(port);
        this.logManager = logManager;
        this.dataBaseManager = dataBaseManager;
    }
    @Override
    public void run(){
        System.out.println("region相互传输监听线程启动成功");
        while (true){
            try {
                Thread.sleep(1000);
                Socket socket = serverSocket.accept();

                //连接后启动相互传输线程
                RegionToRegionSendThread regionToRegionSendThread = new RegionToRegionSendThread(socket, dataBaseManager, logManager);
                Thread socketThread = new Thread(regionToRegionSendThread);
                socketThread.start();
            } catch (InterruptedException | IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
